package com.wuba.rxjavademo.lyz.day0416;

import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.Subscriber;
import rx.observables.ConnectableObservable;

/**
 * Created by liyaxing on 2016/4/27.
 */
public class Demo09 {

    public static void main(String[] ss){

        Demo09 demo09 = new Demo09() ;
//        ConnectableObservable<Long> observable = demo09.retryObservable() ;
//        observable.subscribe(aLong -> print("action1 : " + aLong)) ;
//        try {
//            Thread.sleep(5000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//        observable.connect() ;
//        try {
//            Thread.sleep(5000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//        observable.subscribe(aLong -> print("action2 : " + aLong)) ;
//
//
//        try {
//            Thread.sleep(50000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        Observable<String> observable = demo09.composeObservable() ;
        observable.subscribe(s -> print(s)) ;
    }

    private Observable<String> composeObservable(){
        Observable.Transformer<Integer,String> transformer = integerObservable -> integerObservable.map(integer -> "s : " + integer);
        return Observable.just(1,2,3,4,5).compose(transformer) ;
    }

    private Observable<String> liftObservable(){
        Observable.Operator<String,String> operator = subscriber -> {
            print("---------");
            return new Subscriber<String>() {
                @Override
                public void onCompleted() {

                }

                @Override
                public void onError(Throwable e) {

                }

                @Override
                public void onNext(String s) {
                    subscriber.onNext("myOperator : " + s);
                }
            } ;
        };

        return Observable.just(1,2,3,4).map(integer -> "map : " + integer).lift(operator) ;
    }

    private ConnectableObservable<Long> retryObservable(){
        return Observable.interval(1,TimeUnit.SECONDS).replay() ;
    }

    private ConnectableObservable<Long> publishObservable(){
        return Observable.interval(1, TimeUnit.SECONDS).publish() ;
    }

    private static void print(Object obj){
        System.out.println(obj) ;
    }

}
